Help with Do Loop

The code being written is to take a text string that is itself a comma separated list and extract the individual items, saving them it into a string list ( string [ ] ).  Each item in the comma separated string has quotes around it (e.g., string may be ["Unspecified", "Yes", "No"] and the desired output is to have string [0] = Unspecified, string [1] = Yes and string [2] = No  (all with the quotes {"} removed)). 

This code will be used as part of a larger DXL where this function will be repeated and the comma separated list being worked on will be of variable length (i.e., some will have 2 items, some have as many as 10). 

 The issue is that I do not see how to write the DXL Do loop statement to separate out the individual items. 


Mike.P.Adams - Mon Jan 19 08:50:32 EST 2015

Re: Help with Do Loop
Mathias Mamsch - Tue Jan 20 05:48:38 EST 2015

Should be a simple regular expression search loop. Note that the example below will not care for any commas, just look for the quoted string. If you want to allow escaped quotes inside the quoted strings (like in CSV) the regexp needs to look more complicated like below. Just to give you an idea how to start.

Regards, Mathias

Note that the below would not allow you to END a quoted string with a backslash. To do so you would need to have backslahes also escaped inside the quoted string, which would need slight modifications. So this is just an example of how to deal with quotes inside the quoted strings. If you dont need those, then your regexp will become simpler.

string sTest = "\"Hallo\", \"quoted \\\"World\\\"!\",\"How is it going?\""

print "Text: " sTest "\n"

Skip sk = create(); // place the found items in this 'array'

int cnt = 0, pos = 0; 

// regexp description
// [ \t] -> consume any whitespace before the quoted string
// \\\\\"  --> Consume a backslashed quote
// [^\"]   --> Consume any non quote character
               
string sRegexp = "[ \t]*\"((\\\\\"|[^\"])*)\""

print "Regexp used: " sRegexp "\n"

Regexp re = regexp2 sRegexp

Buffer buf = create(); buf = sTest; 
while (search(re, buf, pos)) {
    string sMatch = buf[pos+start 0:pos + end 0]
    pos += 1 + end 0
    print "Found: " sMatch "\n"
    put (sk, sMatch, cnt++)
}

delete buf

 

Re: Help with Do Loop
Mike.P.Adams - Wed Jan 21 12:41:36 EST 2015

Mathias Mamsch - Tue Jan 20 05:48:38 EST 2015

Should be a simple regular expression search loop. Note that the example below will not care for any commas, just look for the quoted string. If you want to allow escaped quotes inside the quoted strings (like in CSV) the regexp needs to look more complicated like below. Just to give you an idea how to start.

Regards, Mathias

Note that the below would not allow you to END a quoted string with a backslash. To do so you would need to have backslahes also escaped inside the quoted string, which would need slight modifications. So this is just an example of how to deal with quotes inside the quoted strings. If you dont need those, then your regexp will become simpler.

string sTest = "\"Hallo\", \"quoted \\\"World\\\"!\",\"How is it going?\""

print "Text: " sTest "\n"

Skip sk = create(); // place the found items in this 'array'

int cnt = 0, pos = 0; 

// regexp description
// [ \t] -> consume any whitespace before the quoted string
// \\\\\"  --> Consume a backslashed quote
// [^\"]   --> Consume any non quote character
               
string sRegexp = "[ \t]*\"((\\\\\"|[^\"])*)\""

print "Regexp used: " sRegexp "\n"

Regexp re = regexp2 sRegexp

Buffer buf = create(); buf = sTest; 
while (search(re, buf, pos)) {
    string sMatch = buf[pos+start 0:pos + end 0]
    pos += 1 + end 0
    print "Found: " sMatch "\n"
    put (sk, sMatch, cnt++)
}

delete buf

 

Mathias,

Thanks for the help with the DXL code.  One problem with the code (and I am not that fluent in using regular expressions) is that the code stores the individual strings as:

"text1"

"text2"

"text3"

What I need is

text1

text2

text3

   The quotes (") around the text strings need to be removed also.  How does the regular expression change to accomplish this?

 

Re: Help with Do Loop
GregM_dxler - Wed Jan 21 17:11:35 EST 2015

Mike.P.Adams - Wed Jan 21 12:41:36 EST 2015

Mathias,

Thanks for the help with the DXL code.  One problem with the code (and I am not that fluent in using regular expressions) is that the code stores the individual strings as:

"text1"

"text2"

"text3"

What I need is

text1

text2

text3

   The quotes (") around the text strings need to be removed also.  How does the regular expression change to accomplish this?

 

Change the    string sMatch = buf[pos+start 0:pos + end 0] command to

   string sMatch = buf[((pos+start 0) +1):((pos + end 0) -1)]

to eliminate the quotes.  May have to do that in multiple steps if dxl doesn't let you add three numbers together.

Hope this helps,

Greg

 

Re: Help with Do Loop
Mike.P.Adams - Mon Feb 16 12:25:12 EST 2015

GregM_dxler - Wed Jan 21 17:11:35 EST 2015

Change the    string sMatch = buf[pos+start 0:pos + end 0] command to

   string sMatch = buf[((pos+start 0) +1):((pos + end 0) -1)]

to eliminate the quotes.  May have to do that in multiple steps if dxl doesn't let you add three numbers together.

Hope this helps,

Greg

 

Sorry for adding a new topic to this string, but it looks like the Forum changed and I do not see how to log a new question...

I have DXL code that creates a list that needs to be made available in the main Dialog Box (DB) as a pick list.  Everything appears to be working except that the code that is to generate this list does not appear to be executed.  Question is why. 

When this list generating code is run separately, it works as expected.  High level view of code structure is listed below.

 

Initialize variables

  .

  .

Code for other functions

  .

  .

CODE FOR GENERATING LIST SHOWN BETWEEN THE ASTERISKS (*)

********************

current = folder "/"

int cntProj = 0

int intProjLst = 0

 

Item itemRef

for itemRef in current Folder do

{ // First need to know how many projects exist.

            if (type(itemRef) == "Project")

            {

                        cntProj++

            }

}

intProjLst = cntProj

 

if(cntProj == 0)

{ // No projects found

            string lstProjDir [1]

            lstProjDir [0] = "No Projects found..."

}

else

{

            string lstProjDir [intProjLst]

            cntProj = 0  // Resetting counter to start over and store Project names.

            for itemRef in current Project do

            { // With # projects known, now will store names into list.

                        if (type(itemRef) == "Project")

                        {

                                    lstProjDir [cntProj] = name(itemRef)

                                    cntProj++

                        }

            }

} // End of 'if(cntProj == 0)' statement.  .

********************

 

DB Element definition

  .

  .

OnApply Function to handle DB selected button

  .

  .

DB Elements alignment code

show DB Box

 

Re: Help with Do Loop
sekrbo - Wed Feb 18 08:39:51 EST 2015

Mike.P.Adams - Mon Feb 16 12:25:12 EST 2015

Sorry for adding a new topic to this string, but it looks like the Forum changed and I do not see how to log a new question...

I have DXL code that creates a list that needs to be made available in the main Dialog Box (DB) as a pick list.  Everything appears to be working except that the code that is to generate this list does not appear to be executed.  Question is why. 

When this list generating code is run separately, it works as expected.  High level view of code structure is listed below.

 

Initialize variables

  .

  .

Code for other functions

  .

  .

CODE FOR GENERATING LIST SHOWN BETWEEN THE ASTERISKS (*)

********************

current = folder "/"

int cntProj = 0

int intProjLst = 0

 

Item itemRef

for itemRef in current Folder do

{ // First need to know how many projects exist.

            if (type(itemRef) == "Project")

            {

                        cntProj++

            }

}

intProjLst = cntProj

 

if(cntProj == 0)

{ // No projects found

            string lstProjDir [1]

            lstProjDir [0] = "No Projects found..."

}

else

{

            string lstProjDir [intProjLst]

            cntProj = 0  // Resetting counter to start over and store Project names.

            for itemRef in current Project do

            { // With # projects known, now will store names into list.

                        if (type(itemRef) == "Project")

                        {

                                    lstProjDir [cntProj] = name(itemRef)

                                    cntProj++

                        }

            }

} // End of 'if(cntProj == 0)' statement.  .

********************

 

DB Element definition

  .

  .

OnApply Function to handle DB selected button

  .

  .

DB Elements alignment code

show DB Box

 

If the only need for the array is to populate your DBE, then I would do it differently.

First create the Dialogbox and the Element, using a empty dummy string str[] = {""}

Second realize the DB

Third call on a function to fill the DBE, empty the list and then insert each project as you find them...

Finally show the DB...

 

/sekrbo